package com.ingrun.tuboshu.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.ingrun.tuboshu.entity.SysUser;
import com.ingrun.tuboshu.entity.vo.PasswordLoginQ;
import com.ingrun.tuboshu.entity.vo.UpdUserQ;
import com.ingrun.tuboshu.service.IAuthService;
import com.ingrun.tuboshu.utils.Response;
import com.ingrun.tuboshu.utils.StpKit;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    private IAuthService authService;

    // 用户登录
    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public Response<String> login(@RequestBody PasswordLoginQ query) {
        return Response.success(authService.login(query));
    }

    @PostMapping("/logout")
    @Operation(summary = "用户登出")
    public Response<Boolean> logout() {
        return Response.success(authService.logout());
    }

    // 获取登录用户信息
    @GetMapping("/getLoginUserInfo")
    @Operation(summary = "获取登录用户信息")
    @SaCheckLogin(type = "user")
    public Response<SysUser> getLoginUserInfo() {
        return Response.success(authService.getLoginUserInfo());
    }

    // 判断当前用户是否登录
    @GetMapping("/isLogin")
    @Operation(summary = "判断当前用户是否登录")
    public Response<Boolean> isLogin() {
        return Response.success(StpKit.USER.isLogin());
    }

    // 修改自己的用户信息
    @PostMapping("/updateSelfUserInfo")
    @Operation(summary = "修改自己的用户信息")
    @SaCheckLogin(type = "user")
    public Response<Boolean> updateSelfUserInfo(@RequestBody UpdUserQ query) {
        return Response.success(authService.updateSelfUserInfo(query));
    }

}
